package com.example.myapplication.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.bean.IndexListBean
import com.example.myapplication.databinding.IndexListItemBinding

class IndexListAdapterTest(private val data: ArrayList<Map<String, Any>>) :
    RecyclerView.Adapter<IndexListAdapterTest.ViewHolder>() {
    class ViewHolder(private val binding: IndexListItemBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(item: Map<String, Any>, position: Int) {
//            binding.imageView.setImageResource(item[position])
            binding.titleTextView.text = item["title"].toString()
            binding.textView.text = item["content"].toString()
            binding.root.setOnClickListener {
                Toast.makeText(
                    binding.root.context,
                    "你点击了第${position}个list项",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding =
            IndexListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(data[position], position)
    }
}